package com.Controller;

import cn.hutool.core.util.StrUtil;
import com.Bean.User;
import com.Common.CodeMsg;
import com.Common.Constant;
import com.Common.PageInfo;
import com.Common.Result;
import com.Service.GoodService;
import com.Service.impl.GoodServiceImpl;
import com.Util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: hkw14
 * @time: 2021/5/2 23:42
 */
@WebServlet("/good.do")
public class GoodServlet extends HttpServlet {
    private static final long serialVersionUID = 5094669991770713160L;
    GoodService goodService = new GoodServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        try {
            //利用反射调用方法
            this.getClass().getDeclaredMethod(type, HttpServletRequest.class,HttpServletResponse.class).invoke(this, req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String limit = req.getParameter("limit");
        String page = req.getParameter("page");
        String goodName = req.getParameter("goodName");
        String providerName = req.getParameter("providerName");
        String goodsType = req.getParameter("goodsType");
        String brand = req.getParameter("brand");
        Map<String,Object> params = new HashMap<String,Object>();

        if(StrUtil.isNotBlank(goodName)) {
            params.put("goodName", goodName);
        }
        if(StrUtil.isNotBlank(providerName)) {
            params.put("providerName", providerName);
        }
        if(StrUtil.isNotBlank(goodsType)) {
            params.put("goodsType", goodsType);
        }
        if(StrUtil.isNotBlank(brand)) {
            params.put("brand", brand);
        }

        Result result =goodService.queryPage(params, page, limit);
        RespUtil.write(resp,result);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取所有要删除的用户ID
        User user = (User)req.getSession().getAttribute(Constant.CURRENT_USER);
        if (user.getRole() != 1){
            RespUtil.write(resp,new Result(CodeMsg.USER_RIGHTS_ERROR));
            return;
        }
        String[] ids = req.getParameterValues("ID");
        System.out.println(Arrays.toString(ids));
        Result rs = goodService.deleteGood(ids);
        RespUtil.write(resp, rs);
    }

    protected void getList(HttpServletRequest req, HttpServletResponse resp){
        Result providerList = goodService.getGoodList();
        RespUtil.write(resp,providerList);
    }

    protected void addNewGoods(HttpServletRequest req, HttpServletResponse resp){
        User user = (User)req.getSession().getAttribute(Constant.CURRENT_USER);
        if (user.getRole() != 1){
            RespUtil.write(resp,new Result(CodeMsg.USER_RIGHTS_ERROR));
            return;
        }
        String goodsType = req.getParameter("goodsType");
        String brand = req.getParameter("brand");
        String goodsName = req.getParameter("goodsName");
        String providerName = req.getParameter("providerName");
        String description = req.getParameter("description");
        Result result = goodService.newGoods(goodsName, providerName, description, goodsType, brand);
        RespUtil.write(resp,result);
    }

    protected void getByID(HttpServletRequest req, HttpServletResponse resp){
        Integer goodID = new Integer(req.getParameter("goodID"));
        Result result = goodService.getGoodbyID(goodID);
        RespUtil.write(resp,result);
    }
}
